package cate.game.role.pvp.teampvp;

import cate.common.table.d.GDFunc;
import cate.common.table.pvp.teampvp.row.TeamPvpChallengeRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.pvp.teampvp.TeamPvpReport;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.pvp.teampvp.msg.TeamPvpChallengeRewardResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.time.DateKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.*;

@NoteClass("组队竞技")
public class MyTeamPvp extends FuncOpenChecker {
    @NoteField("战报")
    @JsonIgnore
    public List<TeamPvpReport> reports;

    @NoteField("挑战次数, 临时保存一下")
    @Transient
    @JsonIgnore
    public int challengeTimes;

    @NoteField("奖励领取记录")
    public Map<Integer, Boolean> gotMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (reports == null) {
            reports = new ArrayList<>();
        }
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
    }

    @Override
    public int getFuncId() {
        return GDFunc.TEAM_PVP;
    }

    @Override
    public void onAfterOpen() {
        // 同布人物快照
        role.getRes().crossSync.syncSnapshotGroup();
    }

    private synchronized void checkReportsExpire() {
        int day = TimeTool.dayOfWeekCn(System.currentTimeMillis());
        day += 1;
        if (day== Calendar.MONDAY ||
                day == Calendar.THURSDAY ||
                day == Calendar.TUESDAY ||
                day == Calendar.WEDNESDAY) {
            reports.removeIf(e -> e.time <= System.currentTimeMillis() - DateKit.DAY_MS * 7);
        } else {
            reports.removeIf(e -> e.time <= System.currentTimeMillis() - DateKit.DAY_MS * 3);
        }
    }

    public synchronized void addReports(TeamPvpReport report) {
        reports.add(report);
        if (reports.size() > 10) {
            reports.remove(0);
        }
    }

    public GameResult<Void> gotReward(int tid) {
        GameResult<Void> r = new GameResult<>();
        TeamPvpChallengeRow rewardRow = role.getGame().table.teamPvp.challenge.get(tid);
        if (rewardRow == null) {
            return r.fail("奖励不存在");
        }
        if (gotMap.containsKey(tid)) {
            return r.fail("奖励已领取");
        }
        if (challengeTimes < rewardRow.challengeTimes) {
            return r.fail("未达到奖励领取条件");
        }
        gotMap.put(tid, true);
        new MixResAdder()
                .setRes(new MixRes(rewardRow.rewardStr))
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        role.sendNow(new TeamPvpChallengeRewardResp(gotMap));
        return r;
    }

    @Override
    public void onWeekSpan(boolean silence) {
        challengeTimes = 0;
        gotMap.clear();
    }

    @Override
    public void onDaySpan(boolean silence) {
        checkReportsExpire();
    }
}
